package com.example.yan_music.adapter;

import android.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yan_music.R;
import com.example.yan_music.db.table.FavoriteSong;
import com.example.yan_music.listener.OnRcvClickListener;

import java.util.ArrayList;
import java.util.List;

public class RcvFavoriteAdapter extends RecyclerView.Adapter<RcvFavoriteAdapter.ItemHolder> {

    /**
     * 收藏歌曲列表数据源
     */
    private List<FavoriteSong> mFavoriteList = new ArrayList<>();


    public RcvFavoriteAdapter(ArrayList<FavoriteSong> mFavoriteList) {
        this.mFavoriteList = mFavoriteList;
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rcv_favorite_item, parent, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder holder, int position) {
        ItemHolder itemHolder = holder;
        FavoriteSong song = mFavoriteList.get(position);

        itemHolder.position = position;
        // 设置歌曲名
        itemHolder.tv_song_name.setText(song.getSongName());
        // 设置歌手名
        itemHolder.tv_singer_name.setText(song.getSingerName());
    }

    @Override
    public int getItemCount() {
        return mFavoriteList.size();
    }

    /**
     * ViewHolder
     */
    class ItemHolder extends RecyclerView.ViewHolder{
        private int position;
        private TextView tv_song_name, tv_singer_name;

        public ItemHolder(@NonNull View itemView) {
            super(itemView);
            tv_song_name = itemView.findViewById(R.id.tv_favorite_song_name);
            tv_singer_name = itemView.findViewById(R.id.tv_favorite_singer_name);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO 开始播放
                }
            });


        }

    }

}
